<?php

class CompanyController extends BackEndController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array();
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new Company;

        if (isset($_POST['Company'])) {
            $model->attributes = $_POST['Company'];
            $model->zipcode = $_POST['Company']['zipcode'];
            $file = CUploadedFile::getInstance($model, 'picture');
            $year = date('Y');
            $month = date('m');
            $day = date('d');

            if ($file != null) {
                $imageVariables = getimagesize($file->tempName);
                $file = EUploadedImage::getInstance($model, 'picture');
                if ($imageVariables[0] < Yii::app()->params['picture-width']) {
                    $model->addError('piture', 'Vui lòng chọn hình ảnh có chiều dài ít nhất là ' . Yii::app()->params['picture-width'] . 'px');
                    return $this->render('create', array(
                        'model' => $model,
                        'categories' => Category::model()->findAll()
                    ));
                }
                $extension = $file->getExtensionName();
                $fileName = HSCommon::getSEO($model->name) . '_' . rand(10000, 99999) . '.' . $extension;

                $base = HSCommon::checkFolderExisted($year, $month, $day, 'company');

                if ($file->saveAs($base . "/$year/$month/$day/$fileName")) {
                    $model->picture = "/pictures/company/$year/$month/$day/$fileName";

                    if ($model->save()) {
                        $this->redirect(array('index'));
                    }
                } else {
                    $model->addError('picture', 'Không lưu được ảnh');
                }

            } else {
                if ($model->save()) {
                    $this->redirect(array('index'));
                }
            }
        }

        $this->render('create', array(
            'model' => $model,
            'categories' => Category::model()->findAll()
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);
        if (isset($_POST['Company'])) {
            $pic_url = $model->picture;
            $model->attributes = $_POST['Company'];
            $model->zipcode = $_POST['Company']['zipcode'];
            $file = CUploadedFile::getInstance($model, 'picture');
            $year = date('Y');
            $month = date('m');
            $day = date('d');

            if ($file != null) {
                $imageVariables = getimagesize($file->tempName);
                $file = EUploadedImage::getInstance($model, 'picture');
                if ($imageVariables[0] < Yii::app()->params['picture-width']) {
                    $model->addError('piture', 'Vui lòng chọn hình ảnh có chiều dài ít nhất là ' . Yii::app()->params['picture-width'] . 'px');
                    return $this->render('update', array(
                        'model' => $model,
                        'categories' => Category::model()->findAll()
                    ));
                }

                $extension = $file->getExtensionName();
                $fileName = HSCommon::getSEO($model->name) . '_' . rand(10000, 99999) . '.' . $extension;

                $base = HSCommon::checkFolderExisted($year, $month, $day, 'company');

                if ($file->saveAs($base . "/$year/$month/$day/$fileName")) {
                    $model->picture = "/pictures/company/$year/$month/$day/$fileName";

                    if ($model->save()) {
                        $this->redirect(array('index'));
                    }
                } else {
                    $model->addError('picture', 'Không lưu được ảnh');
                }

            } else {
                $model->picture = $pic_url;
                if ($model->save()) {
                    $this->redirect(array('index'));
                }
            }
        }

        $this->render('update', array(
            'model' => $model,
            'categories' => Category::model()->findAll()
        ));
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index'));
    }

    public function actionIndex() {
        $model = new Company('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['Company']))
            $model->attributes = $_GET['Company'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = Company::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

}
